package insight.google.game.era.mgr;

import insight.google.game.era.bo.GameActionLog;
import insight.google.game.era.bo.GameRoom;
import insight.google.game.era.bo.GameUser;
import insight.google.game.era.data.User;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import javax.jdo.annotations.FetchGroup;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
//@FetchGroup(name = "gameLogManager", members = { @Persistent(name = "logroom"),
//		@Persistent(name = "roomLogList") })
public class GameLogManager {

	// private static GameLogManager instance = new GameLogManager();

	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	@PrimaryKey
	private Key id;

	@Persistent(mappedBy = "gameLogManager",defaultFetchGroup="true")
	private GameRoom logroom;

	@Persistent(defaultFetchGroup="true")
	List<GameActionLog> roomLogList = new ArrayList<GameActionLog>();

	public GameLogManager(GameRoom room) {
		this.logroom = room;
	}

	public void addGameLog(GameActionLog actionLog) {
		roomLogList.add(actionLog);
	}

	public void broadcastGameActionLog() {

		Collection<GameUser> userList = logroom.getUserList();
		if (userList != null) {
			for (GameUser user : userList) {
				user.updateGameLog(roomLogList);
			}
		}
	}
	
//	private void brodcast(User currUser){
//		Collection<GameUser> userList = logroom.getUserList();
//		if (userList != null) {
//			for (GameUser user : userList) {
//				if()
//				user.updateGameLog(roomLogList);
//			}
//		}
//	}

	public Key getId() {
		return id;
	}

	// public void setId(Key id) {
	// this.id = id;
	// }

	public GameRoom getRoom() {
		return logroom;
	}

	public void setRoom(GameRoom room) {
		this.logroom = room;
	}

	public List<GameActionLog> getRoomLogList() {
		return roomLogList;
	}

	// public void setRoomLogList(List<GameActionLog> roomLogList) {
	// this.roomLogList = roomLogList;
	// }

	// @Persistent
	// private List<GameActionLog> roomLogList;

	// @NotPersistent
	// HashMap<String, List<GameActionLog>> roomLogList = new HashMap<String,
	// List<GameActionLog>>();

	// public static GameLogManager getInstance(){
	// return instance;
	// }

	// public void addRoomToLogManager(GameRoom room){
	//		
	//			
	// // if(!roomLogList.containsKey(room.getRoomName())){
	// // roomLogList.put(room.getRoomName(), new ArrayList<GameActionLog>());
	// // }
	// }

	// public void removeRoomFromLogManager(GameRoom room){
	// if(!roomLogList.containsKey(room.getRoomName())){
	// roomLogList.remove(room.getRoomName());
	// }
	// }

	// public List<GameActionLog> getGameLogListByRoom(String roomName){
	// List<GameActionLog> roomLogs = this.roomLogList.get(roomName);
	// if(roomLogs != null){
	// return roomLogs;
	// }
	//		
	// return null;
	// }

}
